<template>
  <view>
    <uv-picker ref="pickerCom" :columns="addressList" keyName="name" @change="change" @confirm="confirm">
    </uv-picker>
  </view>
</template>

<script setup lang="ts">
const { proxy } = getCurrentInstance();
const instance = getCurrentInstance();
const emit = defineEmits(["fatherMethods"]);
let provinces = [];
let citys = [];
let areas = [];
let addressList = ref([]);
let currents = [0, 0, 0];

let getInfo = async () => {
  //如果没有省份
  if (!provinces.length) {
    let resp = await proxy.$atlas.getProvincesCitysAreas("", 1);
    provinces = resp[0].districts;
    //console.log("省", provinces);
  }
  let ret = await proxy.$atlas.getProvincesCitysAreas(
    provinces[currents[0]].name,
    2
  );
  citys = ret[0].districts;
  //console.log("市", citys);
  let res = await proxy.$atlas.getProvincesCitysAreas(
    citys[currents[1]].name,
    3
  );
  areas = res[0].districts;
  //console.log("区", areas);
  addressList.value = [provinces, citys, areas];
};
let open = async () => {
  await getInfo();
  instance.refs.pickerCom.open();
};
let change = (e) => {
  const { indexs } = e;
  currents = [...indexs];
  getInfo();
};
let confirm = (e) => {
  let { value } = e;
  emit("fatherMethods", value);
};

defineExpose({ open });
</script>